<template>
  <div class="home-container">
    <el-card class="filter-card">
      <el-form :model="filterForm" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="会议室类型">
              <el-select v-model="filterForm.type" placeholder="请选择">
                <el-option label="全部" value=""></el-option>
                <el-option label="教室型" value="classroom"></el-option>
                <el-option label="圆桌型" value="roundtable"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          
          <el-col :span="12">
            <el-form-item label="座位数">
              <el-select v-model="filterForm.seats" placeholder="请选择">
                <el-option label="全部" value=""></el-option>
                <el-option label="20人以上" value="20"></el-option>
                <el-option label="50人以上" value="50"></el-option>
                <el-option label="100人以上" value="100"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="开始时间">
              <el-date-picker
                v-model="filterForm.start_time"
                type="datetime"
                placeholder="选择开始时间"
                :disabled-date="disabledDate"
                :disabled-hours="disabledHours"
                :disabled-minutes="disabledMinutes"
              />
            </el-form-item>
          </el-col>
          
          <el-col :span="12">
            <el-form-item label="结束时间">
              <el-date-picker
                v-model="filterForm.end_time"
                type="datetime"
                placeholder="选择结束时间"
                :disabled-date="disabledDate"
                :disabled-hours="disabledHours"
                :disabled-minutes="disabledMinutes"
              />
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-form-item label="设备需求">
          <el-checkbox-group v-model="filterForm.equipment">
            <el-checkbox label="projector">投影仪</el-checkbox>
            <el-checkbox label="audio">音响</el-checkbox>
            <el-checkbox label="network">网络</el-checkbox>
            <el-checkbox label="whiteboard">白板</el-checkbox>
            <el-checkbox label="video_conference">视频会议</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="handleFilter" :loading="loading">筛选会议室</el-button>
          <el-button @click="resetFilter">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { filterRooms } from '@/api/rooms'
import { ElMessage } from 'element-plus'

const router = useRouter()
const loading = ref(false)

const filterForm = ref({
  type: '',
  seats: '',
  start_time: '',
  end_time: '',
  equipment: []
})

const disabledDate = (time) => {
  return time.getTime() < Date.now() - 8.64e7
}

const disabledHours = () => {
  return [...Array(24).keys()].filter(h => h < 8 || h >= 21)
}

const disabledMinutes = (hour) => {
  if (hour === 20) {
    return [...Array(60).keys()].filter(m => m > 0)
  }
  return []
}

const handleFilter = async () => {
  if (!filterForm.value.start_time || !filterForm.value.end_time) {
    ElMessage.warning('请选择开始时间和结束时间')
    return
  }
  
  loading.value = true
  try {
    const params = {
      type: filterForm.value.type,
      seats: filterForm.value.seats,
      equipment: filterForm.value.equipment,
      start_time: filterForm.value.start_time.toISOString(),
      end_time: filterForm.value.end_time.toISOString()
    }
    
    const filteredRooms = await filterRooms(params)
    
    router.push({
      path: '/rooms',
      query: { 
        rooms: JSON.stringify(filteredRooms),
        filterParams: JSON.stringify(params)
      }
    })
  } catch (error) {
    console.error('筛选失败:', error)
    ElMessage.error('筛选会议室失败')
  } finally {
    loading.value = false
  }
}

const resetFilter = () => {
  filterForm.value = {
    type: '',
    seats: '',
    start_time: '',
    end_time: '',
    equipment: []
  }
}
</script>

<style scoped>
.home-container {
  padding: 20px;
}

.filter-card {
  margin-bottom: 20px;
}
</style>